import { IntervalTimeUnit } from "../../types/L_Type";
import EventX from "./EventX";

/**通用自定义循环事件抽象类  */
export default class CommonLoopEvent  extends EventX{
 
  /**回调 */
  private runCallback:(hasExecNums:number)=>void
  /**是否无限循环事件 */
  private infinite:boolean

  /**
   * 
   * @param intervalUnit 执行间隔单位
   * @param intervalVal 执行间隔值
   * @param runCallback 回调函数
   * @param loopTimes 循环次数  如果不传递则是无限循环
   */
  constructor(
      intervalUnit: IntervalTimeUnit,
      intervalVal: number,
      runCallback:(hasExecNums:number)=>void,
      loopTimes?:number
  ) {
    //会在run()方法中无限续费 execLastedNums值 实现无限循环效果
      super('COMMON',intervalUnit,intervalVal,loopTimes || 100000)
      if(loopTimes==undefined){
        this.infinite=true
      }else{
        this.infinite=false
      }
      this.runCallback=runCallback
  } 

  public start(): void {

  }

  public run(): void {
    if(this.infinite){
      //实现此事件的无限循环效果
      this.execLastedNums++
    }
    this.runCallback(this.hasExecNums)
  }


  public end(): void {

  }

  
}  